﻿namespace FTchina.Upgrade
{
    using System;
    using System.IO;
    using System.Net;

    public class UpgradeDown
    {
        private string _downPath;
        private string _savaPath;

        public UpgradeDown(string ver, string downPath, string savePath)
        {
            this._downPath = string.Format("http://{0}/Upgrade/{1}/{2}", UpgradeCore.ServerName, ver, downPath);
            this._savaPath = savePath;
        }

        private bool CreatFile()
        {
            if (System.IO.File.Exists(this._savaPath))
            {
                System.IO.File.Delete(this._savaPath);
            }
            System.IO.File.Create(this._savaPath).Close();
            return true;
        }

        public void Down()
        {
            Stream stream = new WebClient().OpenRead(this._downPath);
            if (this.CreatFile())
            {
                byte[] buffer = new byte[0x400];
                int count = stream.Read(buffer, 0, 0x400);
                Stream stream2 = new FileStream(this._savaPath, FileMode.OpenOrCreate, FileAccess.Write);
                while (count > 0)
                {
                    stream2.Write(buffer, 0, count);
                    count = stream.Read(buffer, 0, 0x400);
                }
                stream.Close();
                stream2.Close();
            }
        }

        public long GetLengh()
        {
            return WebRequest.Create(this._downPath).GetResponse().ContentLength;
        }
    }
}

